package com.foxmail.aroundme.boring.module.news.gif;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.Request;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.SizeReadyCallback;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.target.ViewTarget;
import com.foxmail.aroundme.boring.BuildConfig;
import com.foxmail.aroundme.boring.R;
import com.foxmail.aroundme.boring.app.App;
import com.foxmail.aroundme.boring.bean.entry.GIFEntry;
import com.foxmail.aroundme.boring.utils.Config;
import com.foxmail.aroundme.boring.utils.util.GlideGifUtils;
import com.foxmail.aroundme.mutlitype.ItemViewProvider;
import com.socks.library.KLog;

/**
 * Created by gzl on 1/22/17.
 * GIFProvider
 */

public class GIFListProvider extends ItemViewProvider<GIFEntry, GIFListViewHolder> {

    @NonNull
    @Override
    protected GIFListViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {

        View view = inflater.inflate(R.layout.item_gif, parent, false);

        return new GIFListViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull final GIFListViewHolder holder, @NonNull final GIFEntry gifEntry) {

        if (TextUtils.isEmpty(gifEntry.content) || TextUtils.isEmpty(gifEntry.url)) {
            if (BuildConfig.DEBUG) {
                KLog.e(Config.TAG, "gifEntry content or url is null");
            }
            return;
        }

        holder.gif_content.setText(gifEntry.content);
        holder.showTime.setText(gifEntry.upDateTime);
        String url = gifEntry.url;

        String stiff = url.substring(url.length() - 3, url.length());
        //如果后缀是gif
        if (TextUtils.equals(stiff, "gif")) {
            gifEntry.isGif = true;
        }
        //通过后缀判断是否显示Gif的Logo
        if (gifEntry.isGif) {
            holder.gifPlay.setVisibility(View.VISIBLE);
        } else {
            holder.gifPlay.setVisibility(View.GONE);
        }
        //默认开始都不显示
        holder.progressBar.setVisibility(View.GONE);
        holder.gifCover.setVisibility(View.VISIBLE);

        //第一次加载作为bitmap加载
        Glide.with(App.getContext())
                .load(gifEntry.url)
                .asBitmap()
                .fitCenter()
                //这是有Cover图的方式，好处是item高度不会随便变化，缺点是有重叠显示的问题
                //.into(holder.gifCover);
                //没有Cover图的方式，上面的优缺点相反
                .into(holder.gifShow);

        holder.gifPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (holder.gifShow == null) {
                    if (BuildConfig.DEBUG) {
                        KLog.e(Config.TAG, "holder.gifShow is null");
                    }
                    return;
                }

                holder.gifPlay.setVisibility(View.GONE);
                holder.progressBar.setVisibility(View.VISIBLE);

                Glide.with(App.getContext())
                        .load(gifEntry.url)
                        .thumbnail(1f)
                        .error(R.drawable.icon_picture_error)
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                        .crossFade()
                        .fitCenter()
                        .skipMemoryCache(true)
                        .listener(new RequestListener<String, GlideDrawable>() {
                            @Override
                            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                                try {
                                    if (BuildConfig.DEBUG) {
                                        KLog.e(Config.TAG, "load GIF fail, onException = " + e.getMessage());
                                    }
                                } catch (Exception e1) {
                                    if (BuildConfig.DEBUG) {
                                        KLog.e(Config.TAG, "load GIF fail, and Glide Listener is error, e.getMessage() is null");
                                    }
                                }

                                holder.progressBar.setVisibility(View.GONE);
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                                if (BuildConfig.DEBUG) {
                                    KLog.i(Config.TAG, "load GIF success");
                                }
                                holder.gifCover.setVisibility(View.GONE);
                                holder.progressBar.setVisibility(View.GONE);
                                return false;
                            }
                        }).into(holder.gifShow);
            }
        });
    }
}
